<?php

class pdd extends bhCommon {


    public function __construct()
    {
        parent::__construct();
    }



    /**
     * pdd列表
     */
    public function category()
    {
        $url = 'http://gw-api.pinduoduo.com/api/router';
        $data = array(
            'type' =>  'pdd.goods.opt.get',
            'client_id' => getConfig('pdd_client_id'),
            'timestamp' => time(),
            'parent_opt_id' => 0
        );
        $data['sign'] = autograph($data);
        $data = json_decode(http_post($url, $data), true);

        $category = $data['goods_opt_get_response']['goods_opt_list'];
        if ($category) {
            foreach ($category as $k => $value) {
                if ($value['opt_name'] == '充值') {
                    unset($category[$k]);
                    break;
                }
            }
        }


        $this->json(array('category' => $category));
    }


    /**
     * 商品列表
     */
    public function goods()
    {
        $opt_id = $this->get('opt_id');
        $keyword = $this->get('pdd_keyword');
        $page = $this->get('page', 1);
        $url = 'http://gw-api.pinduoduo.com/api/router';
        $data = array(
            'type' =>  'pdd.ddk.goods.search',
            'client_id' => getConfig('pdd_client_id'),
            'timestamp' => time(),
            //'opt_id' => $opt_id,
            'page' => $page,
            'page_size' => 10,
            'sort_type' => 0,
            'with_coupon' => 'true',
            'custom_parameters' => json_encode(['uid' => $this->member['id']])
        );

        if ($keyword) {
            $data['keyword'] = $keyword;
            $check = '催情香水费洛蒙口罩';
            if (stripos($check, $keyword) !== false) {
                $this->json('内容包含敏感信息,修改后再提交', 0);
            }


        } else {
            $data['opt_id'] = $opt_id;
        }

        $data['sign'] = autograph($data);
        $data = json_decode(http_post($url, $data), true);
        $this->json(array('pddGoodsList' => $data['goods_search_response']['goods_list']));
    }


    /**
     * 拼多多详情
     */
    public function detail()
    {
        $goods_id = $this->get('goods_id');

        $pdd_pid = getConfig('pdd_pid');

        $url = 'http://gw-api.pinduoduo.com/api/router';
        $data = array(
            'type' =>  'pdd.ddk.goods.detail',
            'client_id' => getConfig('pdd_client_id'),
            'timestamp' => time(),
            'p_id' => $pdd_pid,
            'goods_id_list' => '[' . $goods_id . ']'
        );

        if ($search_id = $this->get('search_id')) {
            $data['search_id'] = $search_id;
        }

        $data['sign'] = autograph($data);
        $data = json_decode(http_post($url, $data), true);
        $info = $data['goods_detail_response']['goods_details'][0];
        $info['coupon_start_time'] = date('m月d日', $info['coupon_start_time']);
        $info['coupon_end_time'] = date('m月d日', $info['coupon_end_time']);

        $this->json(array('new_det_list' => $info));
    }


    /**
     * 推广链接生成
     */
    public function generate()
    {
        $goods_id = $this->get('goods_id');

        $pdd_pid = getConfig('pdd_pid');

        $url = 'http://gw-api.pinduoduo.com/api/router';
        $data = array(
            'type' =>  'pdd.ddk.goods.promotion.url.generate',
            'client_id' => getConfig('pdd_client_id'),
            'timestamp' => time(),
            'p_id' => $pdd_pid,
            'goods_id_list' => '[' . $goods_id . ']',
            'generate_we_app' => 'true',
            'generate_qq_app' => 'true',
            'custom_parameters' => json_encode(['uid' => $this->member['id']])
        );

        if ($search_id = $this->get('search_id')) {
            $data['search_id'] = $search_id;
        }

        $data['sign'] = autograph($data);
        $data = json_decode(http_post($url, $data), true);
        if (isQq()) {
            json(array('qq_app_info' => $data['goods_promotion_url_generate_response']['goods_promotion_url_list'][0]['qq_app_info']));
        } else {
            json(array('we_app_info' => $data['goods_promotion_url_generate_response']['goods_promotion_url_list'][0]['we_app_info']));
        }
    }


    public function authorization()
    {
        $pdd_pid = getConfig('pdd_pid');
        $url = 'http://gw-api.pinduoduo.com/api/router';

        $data = array(
            'type' => 'pdd.ddk.member.authority.query',
            'client_id' => getConfig('pdd_client_id'),
            'timestamp' => time(),
            'pid' => $pdd_pid,
            'custom_parameters' => json_encode(['uid' => $this->member['id']])
        );
        $data['sign'] = autograph($data);
        $suessData = json_decode(http_post($url, $data), true);

        $bind = $suessData['authority_query_response']['bind'];

        $we_app_info = [];
        if ($bind == 0) {
            $data = array(
                'type' => 'pdd.ddk.rp.prom.url.generate',
                'client_id' => getConfig('pdd_client_id'),
                'timestamp' => time(),
                'p_id_list' => json_encode([$pdd_pid]),
                'channel_type' => 10,
                'custom_parameters' => json_encode(['uid' => $this->member['id']]),
                'generate_qq_app' => 'true',
                'generate_we_app' => 'true',
            );
            $data['sign'] = autograph($data);
            $suessData = json_decode(http_post($url, $data), true);
            $we_app_info = $suessData['rp_promotion_url_generate_response']['url_list'][0]['we_app_info'];
        }
        $data = array(
            'bind' => $bind,
            'we_app_info' => $we_app_info,
        );

        $this->json($data);



    }



    /**
     * 热门推荐
     */
    public function popular()
    {
        $popular_recommend = getConfig('popular_recommend');
        $popular_recommend = $popular_recommend ? json_decode($popular_recommend, true) : '';

        $this->json(array('popular_recommend' => $popular_recommend));
    }

}